package NWish.properties.materials
{
	import NWish.Misc;
	import NWish.json.OrderDict;
	import NWish.properties.Properties;
	
	import away3d.containers.Scene3D;
	
	
	public class MaterialBaseProperties extends Properties 
	{
		private var _materialPropertiesTypes;
		
		public function MaterialBaseProperties(parent,materialPropertiesTypes)
		{
			super(parent);
			
			//
			_materialPropertiesTypes = materialPropertiesTypes;
			
			//
			var _keys
			= 
				['name'
				,'materialNamespace'
				,'mipmap'
				,'smooth'
				,'repeat'
				,'bothSides'
				,new blendModeProperties()
				,new LightsProperties()
				];
			addPropertieKeys('Base',_keys);
			
			var _keysRead
			= 
				['assetType'	
				,'uniqueId'
				,'requiresBlending'
				];
			addPropertieKeys_read('Base',_keysRead);
		}
		
		override public function get needToNotifyCreateEvent()
		{
			return false;
		}
		
		override public function getJSON(obj/*,subMesh = null,scene:Scene3D*/):String
		{
			var subMesh = _materialPropertiesTypes.subMeshIndex;
			
			if(obj)
			{
				if(subMesh != null && subMesh != -1)
				{
					return super.getJSON(obj.subMeshes[subMesh].material/*,scene*/);
				}
				else
					return super.getJSON(obj.material/*,scene*/);
			}
			else
				return '';
		}
		
		override public function apply(obj, properties:String, /*subMesh,*/ scene:Scene3D)
		{
			var subMesh = _materialPropertiesTypes.subMeshIndex;
			
			if(subMesh != null && subMesh != -1)
				super.apply(obj.subMeshes[subMesh].material, properties,scene);
			else
				super.apply(obj.material,properties,scene);
		}
		
		override public function dictToProperties(dict,obj,scene:Scene3D,isloading)
		{
			super.dictToProperties(dict,obj,scene,isloading);
			
			obj.name = Misc.validName(obj.name);
		}
		
		
		
	}
}


import NWish.Interface.ILogger;

import NWish.Misc;
import NWish.json.OrderDict;
import NWish.properties.Properties;

import away3d.containers.Scene3D;
import away3d.lights.LightBase;
import away3d.materials.MaterialBase;

import flash.display.BlendMode;


class blendModeProperties
{
	
	public function blendModeProperties()
	{
	}
	
	//
	public function propertiesToDict(mat:MaterialBase,dict:OrderDict,read_only = false,saving = false)
	{
		if(read_only == false)
		{
			dict.push('blendMode', mat.blendMode);
			
			if(saving == false)
			{
				dict.push(Properties.readMark + 'BlendModes', 
					[BlendMode.NORMAL
						,BlendMode.MULTIPLY
						,BlendMode.ADD
						,BlendMode.ALPHA
					]);
			}
		}
	}
	
	public function dictToProperties(dict,mat:MaterialBase,scene,isloading)
	{
		var blendMode = dict['blendMode'];
		mat.blendMode = blendMode;
	}
}

class LightsProperties
{
	
	public function LightsProperties()
	{
	}
	
	
	public function propertiesToDict(mat:MaterialBase,dict:OrderDict,read_only,saving = false)
	{
		if(read_only == false)
		{
			var lights = [];
			for each(var l in mat.lights)
			{
				lights.push(l.name);
			}
			
			dict.push('lights',lights);
		}
	}
	
	public function dictToProperties(dict,mat:MaterialBase,scene:Scene3D,isloading)
	{
		var lights = dict['lights'];
		
		var ls = [];
		for each(var l in lights)
		{
			var light = Misc.getChildByNameInScene(l,scene);
			
			if(light is LightBase)
				ls.push(light);
			else if(light == null)
				ILogger.log('can not find the light : ' + l);
			else
				ILogger.log('the object is not a light!');
		}
		
		if(ls.length == 0)
			mat.lights = null;
		else
			mat.lights = ls;
	}
}







